<?php
if (@$_POST['open'] != 'please') {
    exit;
}
include_once('../../php/include_all.php');
?>
<div class="row-fluid sortable">
    <div class="box span12 geo-box">
        <div class="box-header well">
            <h2><i class="icon-home"></i>User</h2>
        </div>
        <div class="box-content">
            <a href="#modalwin" data-toggle="modal" class="btn btn-primary">Tambah Data</a>
        </div>
        <div id="modalwin" class="modal hide fade" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">

            <header class="modal-header">
                <a href="#" class="close" data-dismiss="modal">x</a>
                <h3>Data User</h3>
            </header>

            <div class="modal-body">
                <form id="formUSER">
                    <input type="hidden" name="act" value="simpan_user">
                    <input type="hidden" id="id_akun" name="id_akun" value="0"> 

                    <table class="table-form">
                        <tbody>
                            <tr>
                                <td valign="middle">User Level</td>
                                <td valign="middle">:</td>
                                <td>
									<select name="bidang" id="bidang" class="bidang">
                                        <option id="">-Pilih-</option>
                                        <?php
                                            $bidang = get_datas('select * from user_level order by id_uslevel');
                                            foreach($bidang as $bidang){
                                        ?>
                                        <option value="<?php echo $bidang['id_uslevel']?>"><?php echo $bidang['uslevel']?></option>
                                        <?php
                                        }
                                        ?>
                                    </select>
                                </td>
                            </tr>
                            <tr>
                                <td valign="middle">Nama</td>
                                <td valign="middle">:</td>
                                <td>
                                    <select name="bid-skpd" id="bid-skpd" class="bid-skpd">
										<option id="">-pilih -</option>
                                    </select>

                                </td>
                            </tr>
                            <tr>
                                <td valign="middle">Username</td>
                                <td valign="middle">:</td>
                                <td>
                                    <input type="text" id="kode_skpd" name="kode_skpd" />
                                </td>
                            </tr>


                            <tr>
                                <td></td>
                                <td></td>
                                <td>
                                <a class="btn btn-primary btn-simpan-user"><div id="loader" style="margin-top:2px"></div>Simpan</a>
				<span id="result"></span>
							   </td>
                            </tr>
                        </tbody>
                    </table>
                </form>
            </div>
        </div>
        <table class="table table-bordered geo-table table-hover" width="100%">
            <thead> 
                <tr>
                    <td width="5%">No</td>
                    <td width="10%">User Level</td>
                    <td width="20%">Nama SKPD</td>
                    <td width="10%">NIP</td>
                    <td width="10%">Nama</td>
                    <td width="20%">Username</td>
                    <td width="5%">Password</td>
                    <td width="10%">Action</td>
                </tr>
            </thead>
            <tbody id="tampilUSER">
                <?php
                $x = 1;
				$user = get_datas("select id_akun, uslevel,(select nama_skpd from mus_skpd ur where ur.id_skpd=r.id_skpd) as skpd,namaakun, pass, nama_pj, nip_akun, 
(select nama_bdgbapeda from mus_bidang_bappeda b where b.id_bdgbapeda=r.id_bidang) as bidang, 
(select nama_kecamatan from mus_kecamatan k where k.id_kecamatan=r.id_kecamatan) as kecamatan
from user_level u, mus_akun r
where u.id_uslevel=r.id_uslevel order by id_akun");
			   foreach ($user as $user) {
                    ?><tr>
                        <td><?php echo $x ?></td>
                        <td><?php echo $user['uslevel'] ?></td>
                       <td><?php echo $user['skpd']?>
							<?php echo $user['bidang']?>
							<?php echo $user['kecamatan']?></td>
						<td><?php echo $user['namaakun'] ?></td>
                        <td><?php echo $user['pass'] ?></td>
                        <td><?php echo $user['nama_pj'] ?></td>
                        <td><?php echo $user['nip_akun'] ?></td>
                        <td>
                            <a class="btn btn-danger bt-hapus" name="<?php echo $user['id_akun']; ?>"><i class="icon-trash icon-white"></i></a>
                        </td>
                    </tr>
                    <?php
                    $x++;
                }
                ?>
            </tbody>
        </table>    
    </div>
</div>
</div>
<script type="text/javascript">
    var url = 'admin/master_user/user_save.php';
    
    $('.bidang').change(function(){
		source = "admin/master_user/level.php?id="+this.value;
		var coba=$.get(source)
		coba.done(function(data){
			$('#bid-skpd').html(data);
		})
	//	setKode();
		}
	);

 /*   $('.bid-skpd').change(function(){          
        setKode();
    }); */
    
    $('.btn-simpan-user').click(function(){
        var btn = $(this);
        var load = $('#loader');
		var rslt = $('#result');
        
        load.addClass('spinner pull-left');
        btn.removeClass('btn-primary').addClass('btn-info');
        
        var form = $('#formUSER');
        var data = form.serializeArray();
        var post = $.post(url, data);
        post.done(function(res){
            var result = res.split('__');
            
            if(result.length==3){
                if(result[0]=='success'){
                    rslt.html('<font color="green">Data Tersimpan...</font>');
                    btn.removeClass('btn-info').addClass('btn-primary');
                    setTimeout(function(){
                        rslt.html('');
                    },1500);
                
                    var tbody = $('#tampilUSER');
                    tbody.html(result[2]);
                    ready();
             //       alert(result);
                    
                    document.getElementById("bidang").selected = true;
                    document.getElementById("bid-skpd").selected = true;
					document.getElementById("kode_skpd").value=' ';
                    $('#modalwin').modal('hide');
                    
                    
                }else{
                    rslt.html('<font color="red">'+res+'</font>');
                }
            }else{
                rslt.html('<font color="red">'+res+'</font>')
            }
            load.removeClass();
            btn.removeClass('.btn-info').addClass('.btn-primary');
        });
    });
 //   setKode();
   
    function ready(){
     /*  $('.bt-edit').click(function(){
            var id_akun = this.name;
            var form = $('#formUSER');
            var post = $.post(url,{act:'ubah_akun',id_akun:id_akun});            
            post.done(function(res){
                var value = res.split('__');
     //           alert(value);
                $('#id_akun').val(value[0]);
                form.find('select[name="id_uslevel"]').val(value[1]);
                form.find('select[name="id_skpd"]').val(value[2]);
            });
        }); */
        
        $('.bt-hapus').click(function(){
            var btn = $(this);
            var pid = this.name;
            var load = $(this);
			
            load.html('<div class="spinner"></div>');
            bootbox.confirm("Anda akan menghapus data ini? ",
            function(hsl){
                if(hsl == true){
                    if(pid!=0){
                        var post = $.post(url,{act:'hapus_user', id_akun:pid});
                        post.done(function(res){
                            var hasil = res.split('__');
                            if(hasil.length == 2){
                       //         alert(hasil);
                                if(hasil[0]== 'success'){
                                   // rslt.html('<font>data sudah di hapuss... </font>');
                                   // setTimeout(function(){
                                   //     rslt.html('');
                                   // },1500);
                                    var tbody =$('#tampilUSER');
                                    tbody.html(hasil[1]);
                                    ready();
                                }
                            }
                        });
                        load.html('<i class="icon-trash icon-white">');
                    }
                }else{
                    load.html('<i class="icon-trash icon-white">');
                }
            });
        });
     
        
    }
    
    ready();
 //   setKode();
 /*   function setKode(){
        var id_level= $('.bidang').val();    
        var id_bdskpd = $('.bid-skpd').val();  
	//	consule.log(id_bdskpd);
        var post = $.post(url,{act:'ambil',idlevel:id_level,idbdskpd:id_bdskpd});
        post.done(function(res){
            //            alert(res);
            $('#kode_skpd').val(res);
        });
    }
    */
    
</script>